package com.example.annotation;


import org.apache.shardingsphere.api.hint.HintManager;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * step5:注解拦截器器拦截所有的service，根据注解设置当前线程将使用的是哪个数据源
 */
@Aspect
@Component
public class MasterRouteAspect {

    @Around("@annotation(com.example.annotation.MasterRoute)")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        Object result = null;
        try (HintManager hintManager = HintManager.getInstance()) {
            hintManager.setMasterRouteOnly();
            result = pjp.proceed();//执行方法
        }
      return result;
    }
  }